// http://47.106.152.46: 8080/jeecg
const host_svr = 'http://47.106.152.46:8080/jeecg'//主服务器项目访问路径（检测更新，公司编码）
function doGet(url,req, successCallback, errorCallback){
	var token=''
	uni.getStorage({
		key: 'token',
		success: function (res) {
			console.log(JSON.stringify(res));
			token=res.data
		},
		fail:function(error) {
			uni.showModal({
				title: '提示',
				content: '您还没登录，请登录',
				showCancel:false,
				success: function (res) {
					uni.reLaunch({
						url:'../login/login'
					})
				}
			});
			return
		}
	});
	uni.request({
		url: host_svr+url,
		method:'GET',
		data:req,
		header: {
			'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
			'X-Auth-Token': token
		},
		success: (res) => {
			console.log(JSON.stringify(res));
			if(res.statusCode==401){
				uni.showModal({
					title: '提示',
					content: '登录失效，请重新登录',
					showCancel:false,
					success: function (res) {
						uni.reLaunch({
							url:'../login/login'
						})
					}
				});
			}
			if(res.statusCode==404){
				uni.showModal({
					title: '提示',
					content:res.data ,
					showCancel: false,
				});
				return
			}
			successCallback(res.data.responsMsg);
		},
		fail:(error)=>{
			errorCallback(error);
		}
	});
}
function doPost(url,req, successCallback, errorCallback){
	var token=''
	uni.getStorage({
		key: 'token',
		success: function (res) {
			console.log(JSON.stringify(res));
			token=res.data
		},
		fail:function(error) {
			uni.showModal({
				title: '提示',
				content: '您还没登录，请登录',
				showCancel:false,
				success: function (res) {
					uni.reLaunch({
						url:'../login/login'
					})
				}
			});
			return
		}
	});
	uni.request({
		url: host_svr+url,
		data:req,
		method:'POST',
		header: {
			'content-type': 'application/x-www-form-urlencoded' ,//自定义请求头信息
			'X-Auth-Token':token
		},
		success: (res) => {
			console.log(res)
			if(res.statusCode==401){
				uni.showModal({
					title: '提示',
					content: '登录失效，请重新登录',
					showCancel:false,
					success: function (res) {
						uni.reLaunch({
							url:'../login/login'
						})
					}
				});
			}
			if(res.statusCode==404){
				uni.showModal({
					title: '提示',
					content:res.data ,
					showCancel: false,
				});
				return
			}
			
			successCallback(res.data.responsMsg);
		},
		fail:(error)=>{
			console.log(error)
			uni.showToast({
				title: '系统错误',
				duration: 2000
			});
		}
	});
}
//更新上一页数据
function gengxin_pages(id){
	var pages = getCurrentPages();
	console.log('pages',pages)
	if (pages.length > 1) {
		//上一个页面实例对象
		var prePage = pages[pages.length-2];
		console.log('prePage',prePage)
		//关键在这里
		prePage.onLoad({id:id})//id为传给上个界面的印章id
	} 
}
//获取时间
function getBeforeDate(n){//n为你要传入的参数，当前为0，前一天为-1，后一天为1
	let date = new Date();
	let year,month,day,s;
	date.setDate(date.getDate()+n);
	year = date.getFullYear();
	month = date.getMonth()+1;
	day = date.getDate() ;
	s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day) ;
	return s ;
}
//时间转换
function transfor_date(date){
	let d1 = new Date(date); //时间 格式转换
	let start_date=d1.getFullYear() + '-' + (d1.getMonth() + 1) + '-' + d1.getDate()
	return start_date
}
//调用原生文件系统管理器并选取文件获取文件地址
function PickFile(callback, acceptType) {//acceptType为你要查的文件类型"image/*"，"audio/*"，"video/*;image/*"  // intent.setType("image/*");//intent.setType("audio/*"); //选择音频//intent.setType("video/*;image/*"); //选择视频 （mp4 3gp 是android支持的视频格式）
	var CODE_REQUEST = 1000;
	var main = plus.android.runtimeMainActivity();
	if(plus.os.name == 'Android') {
		var Intent = plus.android.importClass('android.content.Intent');
		var intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		if(acceptType) {
			intent.setType(acceptType);
		} else {
			intent.setType("*/*");
		}
		main.onActivityResult = function(requestCode, resultCode, data) {
			if(requestCode == CODE_REQUEST) {
				var uri = data.getData();
				plus.android.importClass(uri);
				var Build = plus.android.importClass('android.os.Build');
				var isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

				var DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');
				// DocumentProvider
				if(isKitKat && DocumentsContract.isDocumentUri(main, uri)) {
						console.log("版本大于 4.4 ");
					// ExternalStorageProvider
					if("com.android.externalstorage.documents" == uri.getAuthority()) {
						var docId = DocumentsContract.getDocumentId(uri);
						var split = docId.split(":");
						var type = split[0];

						if("primary" == type) {
							var Environment = plus.android.importClass('android.os.Environment');
							callback(Environment.getExternalStorageDirectory() + "/" + split[1]);
						} else {
							var System = plus.android.importClass('java.lang.System');
							var sdPath = System.getenv("SECONDARY_STORAGE");
							if(sdPath) {
								callback(sdPath + "/" + split[1]);
							}
						}
					}
					// DownloadsProvider
					else if("com.android.providers.downloads.documents" == uri.getAuthority()) {
						var id = DocumentsContract.getDocumentId(uri);
						var ContentUris = plus.android.importClass('android.content.ContentUris');
						var contentUri = ContentUris.withAppendedId(
						//	Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
						Uri.parse("content://downloads/public_downloads"), id);
						callback(getDataColumn(main, contentUri, null, null));
					}
					// MediaProvider
					else if("com.android.providers.media.documents" == uri.getAuthority()) {
						var docId = DocumentsContract.getDocumentId(uri);
						var split = docId.split(":");
						var type = split[0];

						var MediaStore = plus.android.importClass('android.provider.MediaStore');
						if("image" == type) {
						contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
						} else if("video" == type) {
						contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
						} else if("audio" == type) {
						contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
						}

						var selection = "_id=?";
						var selectionArgs = new Array();
						selectionArgs[0] = split[1];

						callback(getDataColumn(main, contentUri, selection, selectionArgs));
					}
				}
				// MediaStore (and general)
				else if("content" == uri.getScheme()) {
					callback(getDataColumn(main, uri, null, null));
				}
				// File
				else if("file" == uri.getScheme()) {
					callback(uri.getPath());
				}
			}
		}
		main.startActivityForResult(intent, CODE_REQUEST);
	}
}

function getDataColumn(main, uri, selection, selectionArgs) {
		plus.android.importClass(main.getContentResolver());
		let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,
		null);
		plus.android.importClass(cursor);
		if(cursor != null && cursor.moveToFirst()) {
		var column_index = cursor.getColumnIndexOrThrow('_data');
		var result = cursor.getString(column_index)
		cursor.close();
		return result;
		}
		return null;
}
//动态启用下拉刷新isSupport为true师启用false禁用
function switchPullRefresh(isSupport) {
	const pages = getCurrentPages();
	const page = pages[pages.length - 1];
	const currentWebview = page.$getAppWebview();
	currentWebview.setStyle({
		pullToRefresh: {
			support:isSupport,
			style: plus.os.name === 'Android' ? 'circle' : 'default'
		}
	});
} 

module.exports = {
	host_svr,
	doGet,
	doPost,
	gengxin_pages,
	getBeforeDate,
	PickFile,//调用原生文件系统管理器并选取文件获取文件地址
	switchPullRefresh,//动态启用下拉刷新
	transfor_date,//时间转换
}